# -*- coding: utf-8 -*-
#   /**
#   * Copyright (c) 2022 Beijing Jiaotong University
#   * PhotLab is licensed under [Open Source License].
#   * You can use this software according to the terms and conditions of the [Open Source License].
#   * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#   *
#   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#   * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#   * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#   *
#   * See the [Open Source License] for more details.
#   */
#   /**
#   * Author: Zheng Wang
#   * Created: Sep. 25, 2022
#   * Supported by: National Key Research and Development Program of China
#   */

import random
import phot_engine as pe
from engine_test.test_data.fetch_data_util import util
import time

# set random seed
random.seed(19)

SIMULATION_PROJECT_PATH = r"../test_data/GDUT_20240214_blackbox.phtg"

TASK_NUMBER = 5

if __name__ == '__main__':

    vertex_data_list, edge_data_list, global_data_list = util.export_data_from_phtg(
        phtg_filepath=SIMULATION_PROJECT_PATH)

    # generate graph entity
    ge = pe.GraphEntity(scrutiny=True)
    # add vertex,edge,global parameter to graph entity
    for vertex_data in vertex_data_list:
        ge.add_vertex(vertex_data)
    for edge_data in edge_data_list:
        ge.add_edge(edge_data)
    ge.set_system_global_parameter(global_data_list)

    # generate simulation task by loop
    task_list = []
    priority_list = [pe.TaskPriority.LOW, pe.TaskPriority.MIDDLE, pe.TaskPriority.URGENT, pe.TaskPriority.GOD]

    for i in range(1, TASK_NUMBER + 1):
        simulation_task = ge.gen_simulation_task(task_id="test_project_{}".format(i),
                                                 priority=random.choice(priority_list),
                                                 execution_time=None)
        simulation_task.show_task_info()
        task_list.append(simulation_task)

    # generate computing engine
    engine = pe.PhotLabEngine()
    for task in task_list:
        engine.add_task(task.get_task_id(), task)
        print("Simulation Task Status: ", engine.get_task_status(task.get_task_id()))
        engine.calculate_task(task.get_task_id())

    for task in task_list:
        task.show_task_info()
    for task in task_list:
        print("Task id:{},Priority:{},start time:{},end time:{},duration time:{} s".format(
            task.get_task_id(),
            task.get_priority(),
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(task.get_start_executing_time()))),
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(task.get_end_executing_time()))),
            task.get_end_executing_time() - task.get_start_executing_time()
        ))
